package com.google.way2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.google.R;

public class Way2Activity extends Activity {

	public MyAdapter adapter;	
	public ListView listView;
	
	@Override
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.listview_into_scrollview);
		
				listView = (ListView)this.findViewById(R.id.listview_listview_into_scrollview);
		adapter = new MyAdapter(this);
		listView.setAdapter(adapter);
		setListViewHeightBasedOnChildren(listView);
	}
	
	public void setListViewHeightBasedOnChildren(ListView listView) {
		        ListAdapter listAdapter = listView.getAdapter(); 
		        if (listAdapter == null) {
		            return;
		        }		

		        int totalHeight = 0;
		        for (int i = 0; i < listAdapter.getCount(); i++) {
		            View listItem = listAdapter.getView(i, null, listView);
		            listItem.measure(0, 0);
		            totalHeight += listItem.getMeasuredHeight();
		        }
		        ViewGroup.LayoutParams params = listView.getLayoutParams();
		        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		        params.height += 5;//if without this statement,the listview will be a little short
		        listView.setLayoutParams(params);
		    }

}
